#!/bin/bash
while read line; do
eval $line
done < Config/config

#The plotter is called from any CARBLogger driver
#It depends upon the installation of gnuplot and gnuplotfeed
cleanup()
{
    echo "Caught Signal ... cleaning up."
    if !(rm -f Data/.locked > /dev/null); then
        {
                  exit 1
        }
    fi
    kill -9 ${feed_id}
    echo "Done cleanup ... quitting."
     exit 0
  }

########################################################
#	GET THE PATH TO THE CURRENT DRIVER THE PARAMETERS TO PLOT                   #
########################################################
#DIR returns operating directory of the testd script

DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

#########################################################
#	CHECK THE EXISTANCE OF NEEDED RESOURCES				#
#	MJV													#
#########################################################
if !( which feedgnuplot ); then
	{
	echo "No feedgnuplot has been found here"
	echo "exiting"
	exit 1
	}
elif !(which gnuplot); then
	{
 	echo "No gnuplot has been found here"
	echo "exiting"
	exit 1
	}
elif !( which pgrep ); then
	{
	echo "No pgrep...stopping"
	exit 1	
	}
fi
####################################################################################
#	SYNCHRONIZE 60 Sec PLOTTING CYCLE WITH FRONT DISPLAY ~ 5 sec after the top of the minute
#####################################################################################
while [ `date "+%S"` != 25 ]
do
	sleep .1
done


######################################################################
#	GRAB ALL OF THE DISPLAY PARAMETERS AND PLOT THEM
######################################################################
#TRAP CTRL+C
catchup="0"
trap cleanup 1 2 3 6 15
while [ : ]; do
{
#REMOVE---DEBUGGING
#catchup=1
	if [ $catchup -eq 0 ]; then
		{
		
				hour=`date '+%H'`
				minute=`date '+%M'`
				for h in `seq -w 00 ${hour}`
				do
				(
					#cat Data/${saroad}-`date '+%Y'`-`date '+%d'`-`date '+%m'` | grep -o -e '\(_[0-9][0-9]:[0-9][0-9]\:\:\:\| O3_Lamp_Mon=[0-9.]* \| O3_Conc_Mon=[0-9.]* \| Dilluent_Flow=[0-9.]*\| Gas_Flow_Mon=[0-9.]*\)'
						
						for m in `seq -w 00 60`
				   		do
				   			for i in "Samp_Temp=[0-9.]*" "O3_Conc_Mon=[0-9.]*" "Dilluent_Flow=[0-9.]*" "Gas_Flow_Mon=[0-9.]*"
							do
								
								(
									
									grep $h:$m | grep -o "${i}" | awk '{ print $1 }' | cut -d'=' -f2
								)<Data/${saroad}-`date '+%Y'`-`date '+%d'`-`date '+%m'`   
				  			done | awk 'ORS=(FNR%5)?FS:RS' | awk -v ts="$h:$m" '{ print ts" "$0}' 
				  		done
				)
				done
	}
	fi
	catchup=1

	
	
	for i in "Samp_Temp=" "O3_Conc_Mon=" "Dilluent_Flow=" "Gas_Flow_Mon="; 
	do
			if [ `date '+%H:%M'` == "01:00" ]; then
			{
				echo clear
				sleep 60
			}
			else
			{
				 timestamp=`date -d "1 minute ago" '+_%H:%M:::'`
				 ts=`echo $timestamp | sed 's/::://g' | sed 's/_//g'`
				 for i in "Samp_Temp=[0-9.]*" "O3_Conc_Mon=[0-9.]*" "Dilluent_Flow=[0-9.]*" "Gas_Flow_Mon=[0-9.]*" 
				    do
				     	tail -n 1000 Data/${saroad}-`date '+%Y'`-`date '+%d'`-`date '+%m'` | grep ${timestamp} | grep Sabio | grep -o "${i}" | cut -d'=' -f2
				    done | awk 'ORS=(FNR%5)?FS:RS' | awk -v ts="$ts" '{ print ts" "$0}'
				 sleep 60
			}
			fi
	done

}
done 2>/dev/null | feedgnuplot --stream --lines --domain --xlabel 'Time [HH:MM]' --ylabel 'Units' --title 'Sabio 4010D Actual Values'\
 --timefmt '%H:%M' --extracmds 'set format x "%H:%M"; set terminal x11 noraise size 600,400 position 700,0' --points\
  --curvestyle 0 'linewidth 2 linecolor rgb "orange"' --legend 0 "Sample_Temp (C)" --points\
  --curvestyle 1 'linewidth 2 linecolor rgb "red"' --legend 1 "O3_Conc_Mon (ppb)" \
  --curvestyle 2 'linewidth 2 linecolor rgb "green"' --legend 2 "Dilluent_Flow (l/min)" \
  --curvestyle 3 'linewidth 2 linecolor rgb "blue"' --legend 3 "Gas_Flow_Mon (cc/min)"
#00:44 Samp_Temp=32.0 Samp_Press=1119 O3_Conc_Mon=0 Dilluent_Flow=0.177 Gas_Flow_Mon=0.0